package vg.modules.minimap;

import vg.services.graph_view_manager.interfaces.graph_comparison.AGraphComparisonView;
import vg.services.graph_view_manager.interfaces.graph_view.AGraphView;
import vg.services.graph_view_manager.interfaces.graph_view.IGraphRenderer;
import vg.services.graph_view_manager.interfaces.tab.ATab;
import vg.services.graph_view_manager.realization.graph_view.smart_graph_view.realization.actions.DefaultActionAdapter;
import vg.services.plugin_manager.event_and_request.event.AUIEvent;
import vg.services.plugin_manager.event_and_request.event.UIEventChangeTab;
import vg.services.plugin_manager.interfaces.IPlugin;
import vg.services.plugin_manager.realization.PluginParameter;
import vg.services.user_interface_manager.interfaces.AUserInterfaceElement;
import vg.services.user_interface_manager.interfaces.UserInterfaceObserverAdapter;

import javax.swing.*;
import java.awt.*;

/**
 * Mini map plugin.
 *
 * @author Timur Zolotuhin (e-mail: tzolotuhin@gmail.com)
 */
public class MiniMapPlugin extends AUserInterfaceElement implements IPlugin {
    // Swing components
    private JPanel panel;
    private JComponent miniMapView = null;

    // Mutex
    private final Object generalMutex = new Object();

    public MiniMapPlugin() {
        super("Minimap", null);
    }

    @Override
    public void install(final PluginParameter pluginParameter) throws Exception {
        panel = new JPanel(new GridLayout(1,1));

        pluginParameter.userInterfaceManager.addObserver(new UserInterfaceObserverAdapter() {
            @Override
            public void dispatchEvent(AUIEvent event) {
                switch (event.getType()) {
                    case DEF_CHANGE_TAB: {
                        final UIEventChangeTab bufEvent = (UIEventChangeTab) event;
                        final ATab tab = bufEvent.getTab();
                        synchronized (generalMutex) {
                            setNewTab(tab);
                        }
                        break;
                    }

                    case DEF_CHANGE_UI_STYLE: {
                        synchronized (generalMutex) {
                            SwingUtilities.updateComponentTreeUI(panel);
                        }
                        break;
                    }

                    default:break;
                }
            }
        });
        pluginParameter.userInterfaceManager.addElement(this, "minimap");
	}


    @Override
    public JComponent getView() {
        synchronized (generalMutex) {
            return panel;
        }
    }

//======================================================================
//-----------------PRIVATE METHODS--------------------------------------
    private void setNewTab(ATab tab) {
        // delete old minimap
        panel.removeAll();

        if (tab == null)
            return;

        AGraphView graphView = null;
        // if new minimap != null -> setup it
        switch (tab.getTabType()) {
            case ATab.DEF_GRAPH_VIEW_TAB_TYPE:
                graphView = (AGraphView)tab;
                break;

            case ATab.DEF_GRAPH_COMPARISON_TAB_TYPE:
                graphView = ((AGraphComparisonView)tab).getMainGraphView();
                break;

            default:
                break;
        }

        if (graphView != null) {
            graphView.addAction(new DefaultActionAdapter("get mini map action") {
                @Override
                public void action(final IGraphRenderer graphRenderer) {
                    synchronized (generalMutex) {
                        miniMapView = graphRenderer.getMiniMap();
                        if (miniMapView != null) {
                            panel.removeAll();
                            panel.add(miniMapView);
                        }
                        panel.updateUI();
                    }
                }
            });
        }
    }
}
